package util;

import java.util.Properties;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.nio.charset.Charset;

public class Config {

    private final Properties p;
    private final Properties names = new Properties();

    private static Config ourInstance = new Config();

    public String getProperty(String key) {
        return p.getProperty(key);
    }

    private int getInt(String name) {
        return Integer.parseInt(p.getProperty(name));
    }

    public Properties getNames() {
        return names;
    }
    
    public int getFrameWidth() {
        return getInt("frame.width");
    }

    public int getColor(){
        return getInt("line.color");
    }

    public int getFrameHeight() {
        return getInt("frame.height");
    }

    public int getImageWidth() {
        return getInt("image.width");
    }

    public int getImageHeight() {
        return getInt("image.height");
    }

    public int getSketchWidth() {
        return getInt("sketch.width");
    }

    public String getFrameTitle() {
        return p.getProperty("frame.title");
    }

    public int getSketchHeight() {
        return getInt("sketch.height");
    }

    public int getSendPort() {
        return Integer.parseInt(p.getProperty("send.port"));
    }

    public int getReceivePort() {
        return Integer.parseInt(p.getProperty("receive.port"));
    }

    public String getName() {
        return p.getProperty("name");
    }

    public Charset getEncoding() {
        return Charset.forName(p.getProperty("encoding"));
    }

    public int getDelay() {
        return getInt("delay");
    }

    public int getTimeLimit() {
        return getInt("time.limit");
    }

    public static Config getInstance() {
        return ourInstance;
    }

    private Config() {
        p = new Properties();
        try {
            File dir = new File("conf");
            File guiConfig = new File(dir, "gui.properties");
            p.load(new FileReader(new File(dir, "net.properties")));
            p.load(new FileReader(guiConfig));
            names.load(new FileReader(guiConfig));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public long getSleepingTime() {
        return Long.parseLong(p.getProperty("sleeping-time"));
    }
}
